package com.qlm.controller;

import com.jfinal.kit.HttpKit;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.weixin.sdk.api.SnsAccessTokenApi;
import com.qlm.annoation.WeixinAuoth;
import com.qlm.interceptor.WeixinController;

public class LoginController extends WeixinController{
	
	@WeixinAuoth
	public void index(){
		String quString = getRequest().getQueryString();
		if(quString == null){
			renderText("该码无效");
			return;
		}
		String appId = PropKit.get("appid");
		String strBackUrl = PropKit.get("authoUrl") + "/login/g?p="+quString;
		String url = SnsAccessTokenApi.getAuthorizeURL(appId, strBackUrl, false);
		redirect(url);
		return ;
	}
	
	public void g(){
		String p = getPara("p");
		String openId = getOpenId();
		Record user = Db.findFirst("select * from t_admin where openid = ?",openId);
		if(user != null){
			String username = user.getStr("username");
			HttpKit.get(PropKit.get("adminUrl")+"/weixin?p="+p+"&user="+username);
			renderText("正在登录...");
		}else{
			renderText("该用户无效");
		}
	}
}
